#!/bin/bash

# Slackware build script for supermariowar

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20211209 bkw: updated for v2020_04_25 (latest beta). Note that the game
# uses SDL2 by default now.
# 20170302 bkw: use long-format github URL

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=supermariowar
VERSION=${VERSION:-2020_04_25}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

TARVER="$( echo "$VERSION" | sed 's,_,-,g' )"

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$TARVER
tar xvf $CWD/$PRGNAM-$TARVER.tar.gz
cd $PRGNAM-$TARVER
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# upstream ships these as zip files, but 'make install' expects them
# to be unzipped.
unzip data.zip
unzip testmaps.zip

# cmake defines a SMW_DATADIR but the code doesn't actually use it.
sed -i \
  '/RootDataDirectory *= *"data"/s,"data",SMW_DATADIR,' \
  src/common/global.cpp

# -DUSE_PNG_SAVE=ON is listed in CMakeLists.txt, but isn't actually
# implemented in 2.0beta1... screenshots still work, they're saved as
# .bmp instead.

mkdir -p build
cd build
  cmake \
    -DBUILD_STATIC_LIBS=OFF \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DLIB_SUFFIX=${LIBDIRSUFFIX} \
    -DMAN_INSTALL_DIR=/usr/man \
    -DCMAKE_BUILD_TYPE=Release ..
  make VERBOSE=1
  make install/strip DESTDIR=$PKG
cd ..

# for some reason, the executables are being installed without
# the executable bits set...
chmod 0755 $PKG/usr/games/*

# new-style icons. tiny ones, since that's all they gave us.
# leave the .xpm icons in pixmaps.
mkdir -p $PKG/usr/share/icons/hicolor/32x32/apps
mv $PKG/usr/share/pixmaps/*.png $PKG/usr/share/icons/hicolor/32x32/apps

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
# get rid of the CRLFs in the docs
for i in CHANGELOG CREDITS README.md docs/*.html docs/*.txt; do
  sed 's,\r,,g' "$i" > $PKG/usr/doc/$PRGNAM-$VERSION/"$( basename "$i" )"
done
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
